package com.intellagric.service.traceability.Impl.trace_produce;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceProduceCategoryMapper;
import com.intellagric.pojo.TraceProduceCategory;
import com.intellagric.pojo.TraceProduceCategoryExample;
import com.intellagric.service.traceability.trace_produce.ProduceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/31
 * @DESC： 溯源农产品分类管理服务实现
 */
@Service
public class ProduceCategoryServiceImpl implements ProduceCategoryService {

    @Autowired
    private TraceProduceCategoryMapper produceCategoryMapper;

    /**
     * 增加农产品分类信息
     * @param produceCategory
     * @return
     */
    @Override
    public ResponseMessage addProduceCategory(TraceProduceCategory produceCategory) {
        if (produceCategoryMapper.insertSelective(produceCategory) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除农产品分类信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteProduceCategory(String id) {
        if (produceCategoryMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑农产品分类信息
     * @param produceCategory
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editProduceCategory(TraceProduceCategory produceCategory) {
        if (produceCategoryMapper.updateByPrimaryKeySelective(produceCategory) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询农产品分类信息
     * @param id
     * @return TraceProduceCategory
     */
    @Override
    public TraceProduceCategory getProduceCategoryById(String id) {
        return produceCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过父id查询农产品分类信息
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceCategoryByParentId(String parentId) {
        TraceProduceCategoryExample produceCategoryExample = new TraceProduceCategoryExample();
        produceCategoryExample.setOrderByClause("sort");
        produceCategoryExample.createCriteria().andParentEqualTo(parentId);
        List<TraceProduceCategory> produceCategoryList = produceCategoryMapper.selectByExample(produceCategoryExample);
        return LayuiDataGridResult.success().add(produceCategoryList,produceCategoryList.size());
    }

    /**
     * 查询农产品分类信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceCategoryList() {
        TraceProduceCategoryExample produceCategoryExample = new TraceProduceCategoryExample();
        produceCategoryExample.setOrderByClause("sort");
        produceCategoryExample.createCriteria().andIdIsNotNull();
        List<TraceProduceCategory> produceCategoryList = produceCategoryMapper.selectByExample(produceCategoryExample);
        return LayuiDataGridResult.success().add(produceCategoryList,produceCategoryList.size());
    }

    /**
     * 查询农产品分类信息列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProduceCategoryTree() {
        //查询
        List<TraceProduceCategory> produceCategoryList = this.getProduceCategoryList().getData();

        //查询信息
        List<TraceProduceCategory> rootProduceCategoryList = this.getProduceCategoryByParentId("0").getData();

        for (TraceProduceCategory produceCategory : rootProduceCategoryList) {
            produceCategory.setChildren(getChild(produceCategory.getId(), produceCategoryList));
        }
        return LayuiDataGridResult.success().add(rootProduceCategoryList,rootProduceCategoryList.size());
    }

    private List<TraceProduceCategory> getChild(String id,List<TraceProduceCategory> produceCategoryList) {
        // 子菜单
        List<TraceProduceCategory> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (TraceProduceCategory produceCategory : produceCategoryList) {
            if (produceCategory.getParent() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (produceCategory.getParent().equals(id)) {
                    childList.add(produceCategory);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (TraceProduceCategory produceCategory : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            produceCategory.setChildren(getChild(produceCategory.getId(), produceCategoryList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    @Override
    public LayuiDataGridResult getProduceCategoryTree(String createBy) {
        TraceProduceCategoryExample produceCategoryExample = new TraceProduceCategoryExample();
        produceCategoryExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduceCategory> produceCategoryList = produceCategoryMapper.selectByExample(produceCategoryExample);
        TraceProduceCategoryExample example=new TraceProduceCategoryExample();
        example.createCriteria().andCreateByEqualTo(createBy).andParentEqualTo("0");
        //查询信息
        List<TraceProduceCategory> rootProduceCategoryList = produceCategoryMapper.selectByExample(example);
        for (TraceProduceCategory produceCategory : rootProduceCategoryList) {
            produceCategory.setChildren(getChild(produceCategory.getId(), produceCategoryList));
        }
        return LayuiDataGridResult.success().add(rootProduceCategoryList,rootProduceCategoryList.size());
    }
}
